package com.gongpingjialoan.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.RecordBean;

import java.util.List;

/**
 * Created by Administrator on 2016/6/22.
 */
public class VipCarAdapter extends BaseAdapter {

    private LayoutInflater mInflater;

    private Context context;

    public List<RecordBean.DataEntity.VipBuyEntity> mCarSourceList;

    //用途    (多选)
    private String[] show_intent = {"微型车", "小型车", "中级车", "中高级车", "SUV", "跑车", "豪华车", "商务车"};
    private String[] need_intent = {"mini_car", "small_car,compact_car", "middle_car", "middle_super_car", "suv", "super_car", "luxury_car", "mpv"};

    public VipCarAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<RecordBean.DataEntity.VipBuyEntity> CarSourceList) {
        this.mCarSourceList = CarSourceList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mCarSourceList == null) {
            return 0;
        }
        return mCarSourceList.size();
    }

    @Override
    public Object getItem(int position) {
        return mCarSourceList.get(position);
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        CarSourceViewHolder holder = null;
        IntentViewHolder intentViewHolder = null;
        int type = getItemViewType(position);

        if (convertView == null) {
            if (type == 0) {
                convertView = mInflater.inflate(R.layout.vip_car_item, null);
                holder = new CarSourceViewHolder();
                holder.othersTextView = (TextView) convertView.findViewById(R.id.others);
                holder.functionsTextView = (TextView) convertView.findViewById(R.id.function);
                holder.tvCarPrice = (TextView) convertView.findViewById(R.id.price);
                holder.intentsTextView = (TextView) convertView.findViewById(R.id.intent);
                holder.pushTypeTextView = (TextView) convertView.findViewById(R.id.push);
                convertView.setTag(holder);
            } else {
                convertView = mInflater.inflate(R.layout.vip_car_item1, null);
                intentViewHolder = new IntentViewHolder();
                intentViewHolder.brandTextView = (TextView) convertView.findViewById(R.id.brand);
                intentViewHolder.tvCarPrice = (TextView) convertView.findViewById(R.id.price);
                intentViewHolder.timesTextView = (TextView) convertView.findViewById(R.id.time);
                intentViewHolder.pushTypeTextView = (TextView) convertView.findViewById(R.id.push);
                convertView.setTag(intentViewHolder);
            }
        } else {
            if (type == 0) {
                holder = (CarSourceViewHolder) convertView.getTag();
            } else {
                intentViewHolder = (IntentViewHolder) convertView.getTag();
            }
        }

        RecordBean.DataEntity.VipBuyEntity vipBuyEntity = mCarSourceList.get(position);

        if (type == 0) {
//            if (!TextUtils.isEmpty(vipBuyEntity.getPrice_l()) && !TextUtils.isEmpty(vipBuyEntity.getPrice_h())) {
//                holder.tvCarPrice.setText(vipBuyEntity.getPrice_l() + "-" + vipBuyEntity.getPrice_h() + "万");
//            } else if (TextUtils.isEmpty(vipBuyEntity.getPrice_l()) && !TextUtils.isEmpty(vipBuyEntity.getPrice_h())) {
//                holder.tvCarPrice.setText(vipBuyEntity.getPrice_h() + "万以内");
//            } else if (!TextUtils.isEmpty(vipBuyEntity.getPrice_l()) && TextUtils.isEmpty(vipBuyEntity.getPrice_h())) {
//                holder.tvCarPrice.setText(vipBuyEntity.getPrice_l() + "万以上");
//            } else {
//                holder.tvCarPrice.setText("不限");
//            }
            if(!TextUtils.isEmpty(vipBuyEntity.getPrice())){
                holder.tvCarPrice.setText(vipBuyEntity.getPrice() + "万左右");
            }

            List<String> intents = vipBuyEntity.getVehicle_model();
            if (intents.contains("small_car") || intents.contains("compact_car")) {
                intents.add("small_car,compact_car");
            }
            StringBuilder intentStringBuilder = new StringBuilder();
            for (int i = 0; i < intents.size(); i++) {
                for (int j = 0; j < need_intent.length; j++) {
                    if (intents.get(i).equals(need_intent[j])) {
                        if (i == intents.size() - 1) {
                            intentStringBuilder.append(show_intent[j]);
                        } else {
                            intentStringBuilder.append(show_intent[j] + ",");
                        }
                    }
                }
            }
            holder.intentsTextView.setText(intentStringBuilder.toString());


            List<String> uses = vipBuyEntity.getUses();
            StringBuilder useStringBuilder = new StringBuilder();
            for (int i = 0; i < uses.size(); i++) {
                if (i == uses.size() - 1) {
                    useStringBuilder.append(uses.get(i));
                } else {
                    useStringBuilder.append(uses.get(i) + ",");
                }
            }
            holder.functionsTextView.setText(useStringBuilder.toString());


            List<String> others = vipBuyEntity.getOther_recommend();
            StringBuilder otherStringBuilder = new StringBuilder();
            for (int i = 0; i < others.size(); i++) {
                if (i == others.size() - 1) {
                    otherStringBuilder.append(others.get(i));
                } else {
                    otherStringBuilder.append(others.get(i) + ",");
                }
            }
            holder.othersTextView.setText(TextUtils.isEmpty(otherStringBuilder.toString()) ? "不限" : otherStringBuilder.toString());


            if (vipBuyEntity.getPush_setting().equals("im")) {
                holder.pushTypeTextView.setText("找到立即推送");
            } else {
                holder.pushTypeTextView.setText(vipBuyEntity.getStart_time() + "-" + vipBuyEntity.getEnd_time());
            }
        } else {
            if(vipBuyEntity.getModels() == null){
                intentViewHolder.brandTextView.setText("不限");
            }else{
                int size = vipBuyEntity.getModels().size();
                String data = "";
                for (int i = 0; i < size; i++) {
                    if(i == size - 1){
                        data = data + vipBuyEntity.getModels().get(i);
                    }else{
                        data = data + vipBuyEntity.getModels().get(i) + ",";
                    }
                    intentViewHolder.brandTextView.setText(data);
                }
            }
            intentViewHolder.tvCarPrice.setText(vipBuyEntity.getPrice() + "万左右");
            if (vipBuyEntity.getPush_setting().equals("im")) {
                intentViewHolder.pushTypeTextView.setText("找到立即推送");
            } else {
                intentViewHolder.pushTypeTextView.setText(vipBuyEntity.getStart_time() + "-" + vipBuyEntity.getEnd_time());
            }
            intentViewHolder.timesTextView.setText(vipBuyEntity.getExpect_period());
        }

        return convertView;
    }


    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if ("ambiguous".equals(mCarSourceList.get(position).getType())) {
            return 0;
        } else {
            return 1;
        }
    }

    public class CarSourceViewHolder {
        public TextView tvCarPrice;
        public TextView othersTextView;
        public TextView functionsTextView;
        public TextView intentsTextView;
        public TextView pushTypeTextView;
    }

    public class IntentViewHolder {
        public TextView tvCarPrice;
        public TextView brandTextView;
        public TextView timesTextView;
        public TextView pushTypeTextView;
    }
}
